<template>
  <div class="app-container home">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="知识库名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入知识库名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="知识库分类" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="知识库分类"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.knowledge_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否开启同步" prop="openRagSync">
        <el-select
          v-model="queryParams.openRagSync"
          placeholder="是否开启同步"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.knowledge_sync_switch"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
          >
          <!-- v-hasPermi="['knowledge:base:add']" -->
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-row class="knowledgeBaseContent" :gutter="10">
      <el-col
        v-for="item in baseList"
        :key="item.key"
        :xs="12"
        :sm="12"
        :md="8"
        :lg="6"
        :xl="6"
        class="knowledgeBaseItem"
      >
        <div class="grid-content">
          <el-descriptions :title="item.name" :column="1">
            <template #title>
              <svg-icon
                style="width: 18px; height: 18px"
                :icon-class="dict.type.knowledge_type[item.type].raw.cssClass"
              />
              <span>{{ item.name }}</span>
            </template>
            <el-descriptions-item label="知识库分类">
              {{ getLabelfromValue(dict.type.knowledge_type, item.type) }}
              <!-- {{ dict.type.knowledge_type[item.type].label }} -->
            </el-descriptions-item>
            <el-descriptions-item label="创建时间">{{
              item.createTime
            }}</el-descriptions-item>
            <el-descriptions-item label="是否开启同步">
              <span :style="{ color: corlorList[item.openRagSync] }">
                {{
                getLabelfromValue(
                  dict.type.knowledge_sync_switch,
                  item.openRagSync
                )
              }}
                <!-- {{ dict.type.knowledge_sync_switch[item.openRagSync] }}{{ item.openRagSync }} -->
              </span>
            </el-descriptions-item>
            <el-descriptions-item label="解析算法">
              {{
                getParseLabelfromValue(item.parseType)
              }}
            </el-descriptions-item>
            <el-descriptions-item contentClassName='text-container' label="描述">{{
              item.description
            }}</el-descriptions-item>
          </el-descriptions>
          <div class="knowledgeBaseItem-bottom">
            <el-link
              @click="handleView(item.id)"
              ><i class="iconfont icon-a-qietu327"></i>预览</el-link
              >
              <!-- v-hasPermi="['knowledge:base:view']" -->
            <el-link
              icon="el-icon-delete"
              @click="handleDelete(item)"
              >删除</el-link
              >
              <!-- v-hasPermi="['knowledge:base:remove']" -->
            <el-link
              icon="el-icon-edit"
              @click="handleUpdate(item)"
              >编辑</el-link
              >
              <!-- v-hasPermi="['knowledge:base:edit']" -->
            <el-link
              v-show="item.openRagSync == 1"
              icon="el-icon-refresh"
              @click="handleSync(item)"
              >同步</el-link
              >
              <!-- v-hasPermi="['knowledge:base:sync']" -->
          </div>
        </div>
      </el-col>
    </el-row>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="知识库名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入知识库名称" />
        </el-form-item>
        <el-form-item label="知识库分类" prop="type">
          <el-select
            v-model="form.type"
            placeholder="知识库分类"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.knowledge_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="解析算法" prop="parseType">
          <el-select
            v-model="form.parseType"
            placeholder="解析算法"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="item in parsteSelect"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否开启同步" prop="openRagSync">
          <el-radio-group v-model="form.openRagSync">
            <el-radio
              v-for="dict in dict.type.knowledge_sync_switch"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            type="textarea"
            placeholder="请输入描述"
            v-model="form.description"
            show-word-limit
          >
          </el-input>
          <!-- <el-input v-model="form.description" placeholder="请输入描述" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getParseList,
  listBase,
  delBase,
  addBase,
  syncBase,
  updateBase,
} from "@/api/knowledge/base";
import { getLabel } from "@/utils/index";
export default {
  name: "Index",
  dicts: ["knowledge_type", "knowledge_sync_switch"],
  data() {
    return {
      corlorList: ["#FA5151", "#1890FF"],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 知识库表格数据
      baseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRange: [],
      // 表单参数
      form: {},
      parsteSelect:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        type: undefined,
        openRagSync: undefined,
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "知识库名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "用户名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    // deptName(val) {
    //   this.$refs.tree.filter(val);
    // }
  },
  created() {
    this.getParsteSelect();
  },
  methods: {
    // 字典显示的方法
    getLabelfromValue(dict, value) {
      return getLabel(dict, value);
    },
    // 字典显示的方法
    getParseLabelfromValue(value) {
      if (!value) return '';
      const parste = this.parsteSelect.filter(item => {
        return item.id===value
      })
      return parste&&parste.length>0&&parste[0].name
    },
    /** 查询用户列表 */
    getParsteSelect() {
      getParseList().then(
        (response) => {
          this.parsteSelect = response.rows;
          this.getList()
          // console.log('parsteSelect',this.parsteSelect)
        }
      );
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listBase(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.baseList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        description: null,
        type: "0",
        openRagSync: "0",
        parseType: 'contend_with_img',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleExport":
          this.handleExport(row);
          break;
        default:
          break;
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBase(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBase(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加知识库";
    },
    /** 预览按钮操作 */
    handleView(id) {
      this.$router.push("/knowledge/manage-view/index/" + id);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      row.openRagSync = row.openRagSync + "";
      this.form = row;
      this.open = true;
      this.title = "修改知识库";
    },
    /** 同步按钮操作 */
    handleSync(row) {
      const that = this
      this.$modal.confirm("是否确认同步知识库？").then(function () {
        const param = {
          baseName: row.name,
          baseId: row.id,
        };
        syncBase(param).then((res) => {
          if (res.code == 200) {
            that.$modal.msgSuccess(res.msg);
          }
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除知识库名称为"' + row.name + '"的数据项？')
        .then(function () {
          return delBase(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "knowledge/base/export",
        {
          ...this.queryParams,
        },
        `base_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped lang="scss">
.el-row.knowledgeBaseContent {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }

  .knowledgeBaseItem {
    border-radius: 4px;
    margin-bottom: 10px;

    .grid-content {
      border-radius: 12px;
      min-height: 36px;
      // background: #f6f7ff;
      border: 1px solid #e5e5e5;
      box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.1);
      padding: 10px;
      padding-bottom: 0;
    }
  }
}

.knowledgeBaseItem-bottom {
  padding: 8px;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  .el-link.el-link--default {
    margin-left: 4px;
  }
}
</style>
<style lang="scss">
.grid-content {
  font-size: 16px;
  color: #0e0e0e;
  line-height: 22px;
  .el-descriptions__header {
    padding-bottom: 10px;
    margin-bottom: 6px;
    border-bottom: 1px solid #f1f1f1;
    padding-left: 10px;
  }
  .el-descriptions__body {
    border-bottom: 1px solid #f1f1f1;
    padding-bottom: 10px;
    // height: 150px;
    .el-descriptions-row {
      .el-descriptions-item__cell {
        padding-bottom: 4px;
      }
    }
    .text-container {
      /* 必须设置为弹性布局或者表格单元格布局 */
      display: -webkit-box;
      /* 规定子元素的排列方向 */
      -webkit-box-orient: vertical;
      /* 限制显示的行数为 2 */
      -webkit-line-clamp: 1;
      /* 隐藏溢出内容 */
      overflow: hidden;
      /* 文本溢出时用省略号表示 */
      text-overflow: ellipsis;
      // width: 200px;
      /* 这里可以根据需求调整宽度 */
    }
  }
  .el-descriptions__title {
    font-size: 16px;
    color: #0e0e0e;
    line-height: 22px;
    font-weight: 400;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
    .svg-icon {
      margin-right: 8px;
    }
  }
}
</style>